package com.jxchexie.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

import android.util.LruCache;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

class  ScreenshotsShare {

//    public static Bitmap shotRecyclerView(RecyclerView recyclerView) {
//        if (recyclerView == null) {
//            return null;
//        }
//        try {
//            HolidayAdminAdapter adapter = (HolidayAdminAdapter) recyclerView.getAdapter();
//            Bitmap bigBitmap = null;
//            if (adapter != null) {
//                int size = adapter.getItemCount();
//                int height = 0;
//                Paint paint = new Paint();
//                final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
//
//                // Use 1/8th of the available memory for this memory cache.
//                final int cacheSize = maxMemory / 8;
//                LruCache<String, Bitmap> bitmapCache = new LruCache<>(cacheSize);
//                SparseIntArray bitmapLeft = new SparseIntArray(size);
//                SparseIntArray bitmapTop = new SparseIntArray(size);
//                for (int i = 0; i < size; i++) {
//                    RecyclerView.ViewHolder holder = adapter.createViewHolder(recyclerView, adapter.getItemViewType(i));
//                    adapter.onBindViewSync(holder, i);
//                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
//                    holder.itemView.measure(
//                            View.MeasureSpec.makeMeasureSpec(recyclerView.getWidth() - layoutParams.leftMargin - layoutParams.rightMargin, View.MeasureSpec.EXACTLY),
//                            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
//                    );
//                    holder.itemView.layout(
//                            layoutParams.leftMargin,
//                            layoutParams.topMargin,
//                            holder.itemView.getMeasuredWidth() + layoutParams.leftMargin,
//                            holder.itemView.getMeasuredHeight() + layoutParams.topMargin
//                    );
//                    holder.itemView.setDrawingCacheEnabled(true);
//                    holder.itemView.buildDrawingCache();
//                    Bitmap drawingCache = holder.itemView.getDrawingCache();
//                    if (drawingCache != null) {
//                        bitmapCache.put(String.valueOf(i), drawingCache);
//                    }
//
//                    height += layoutParams.topMargin;
//                    bitmapLeft.put(i, layoutParams.leftMargin);
//                    bitmapTop.put(i, height);
//                    height += holder.itemView.getMeasuredHeight() + layoutParams.bottomMargin;
//                }
//
//                bigBitmap = Bitmap.createBitmap(recyclerView.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
//                Canvas bigCanvas = new Canvas(bigBitmap);
//                Drawable lBackground = recyclerView.getBackground();
//                if (lBackground instanceof ColorDrawable) {
//                    ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
//                    int lColor = lColorDrawable.getColor();
//                    bigCanvas.drawColor(lColor);
//                }
//
//                for (int i = 0; i < size; i++) {
//                    Bitmap bitmap = bitmapCache.get(String.valueOf(i));
//                    bigCanvas.drawBitmap(bitmap, bitmapLeft.get(i), bitmapTop.get(i), paint);
//                    bitmap.recycle();
//                }
//            }
//            return bigBitmap;
//        } catch (OutOfMemoryError oom) {
//            return null;
//        }
//    }
    /**
     *
     */
    public static Bitmap shotRecyclerView2(RecyclerView view) {
        RecyclerView.Adapter adapter = view.getAdapter();
        Bitmap bigBitmap = null;
        if (adapter != null) {
            int size = adapter.getItemCount();
            int height = 0;
            Paint paint = new Paint();
            int iHeight = 0;
            final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
            // Use 1/8th of the available memory for this memory cache.
            final int cacheSize = maxMemory / 8;
            //这个地方用LruCache显得高大上，实际上用个List也是可以的
            LruCache<String, Bitmap> bitmapCache = new LruCache<>(cacheSize);
            for (int i = 0; i < size; i++) {
                RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
                adapter.onBindViewHolder(holder, i);
                holder.itemView.measure(
                        View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
                        holder.itemView.getMeasuredHeight());
                holder.itemView.setDrawingCacheEnabled(true);
                holder.itemView.buildDrawingCache();
                Bitmap drawingCache = holder.itemView.getDrawingCache();
                if (drawingCache != null) {
                    bitmapCache.put(String.valueOf(i), drawingCache);
                }
                height += holder.itemView.getMeasuredHeight();
            }
            // 这个地方容易出现OOM，关键是要看截取RecyclerView的展开的宽高
            bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
            Canvas bigCanvas = new Canvas(bigBitmap);
            Drawable lBackground = view.getBackground();
            //先画RecyclerView的背景色
            if (lBackground instanceof ColorDrawable) {
                ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
                int lColor = lColorDrawable.getColor();
                bigCanvas.drawColor(lColor);
            }
            for (int i = 0; i < size; i++) {
                Bitmap bitmap = bitmapCache.get(String.valueOf(i));
                bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
                iHeight += bitmap.getHeight();
                bitmap.recycle();
            }
        }
        return bigBitmap;
    }


}
